package com.tchintech.pay.utils;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by Administrator on 2017/7/13 0013.
 */

public class EditUtil {

    static String beforeStr = "";
    static String afterStr = "";
    static String changeStr = "";
    static int index = 0;
    static boolean changeIndex = true;

    /**
     * 银行卡监听
     * 用于在输入的时候
     *
     * @param et
     */
    public static void initListener(final EditText et) {
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                beforeStr = s.toString();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                afterStr = s.toString();
                if (changeIndex)
                    index = et.getSelectionStart();
            }

            @Override
            public void afterTextChanged(Editable s) {
                if ("".equals(s.toString()) || s.toString() == null || beforeStr.equals(afterStr)) {
                    changeIndex = true;
                    return;
                } else if (" ".equals(s.toString())) {
                }
                changeIndex = false;
                char c[] = s.toString().replace(" ", "").toCharArray();
                changeStr = "";
                for (int i = 0; i < c.length; i++) {
                    changeStr = changeStr + c[i] + ((i + 1) % 4 == 0 && i + 1 != c.length ? " " : "");
                }
                if (afterStr.length() > beforeStr.length()) {
                    if (changeStr.length() == index + 1) {
                        index = changeStr.length() - afterStr.length() + index;
                    }
                    if (index % 5 == 0 && changeStr.length() > index + 1) {
                        index++;
                    }
                } else if (afterStr.length() < beforeStr.length()) {
                    if ((index + 1) % 5 == 0 && index > 0 && changeStr.length() > index + 1) {
                        //  index--;
                    } else {
                        index = changeStr.length() - afterStr.length() + index;
                        if (afterStr.length() % 5 == 0 && changeStr.length() > index + 1) {
                            index++;
                        }
                    }
                }
                et.setText(changeStr);
                et.setSelection(index);

            }
        });
    }

    /**
     * 银行卡监听
     * 用于在显示的时候
     *
     * @param et
     */
    public static void initTextListener(final TextView et) {
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                beforeStr = s.toString();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                afterStr = s.toString();
                if (changeIndex)
                    index = et.getSelectionStart();
            }

            @Override
            public void afterTextChanged(Editable s) {
                if ("".equals(s.toString()) || s.toString() == null || beforeStr.equals(afterStr)) {
                    changeIndex = true;
                    return;
                } else if (" ".equals(s.toString())) {
                }
                changeIndex = false;
                char c[] = s.toString().replace(" ", "").toCharArray();
                changeStr = "";
                for (int i = 0; i < c.length; i++) {
                    changeStr = changeStr + c[i] + ((i + 1) % 4 == 0 && i + 1 != c.length ? " " : "");
                }
                if (afterStr.length() > beforeStr.length()) {
                    if (changeStr.length() == index + 1) {
                        index = changeStr.length() - afterStr.length() + index;
                    }
                    if (index % 5 == 0 && changeStr.length() > index + 1) {
                        index++;
                    }
                } else if (afterStr.length() < beforeStr.length()) {
                    if ((index + 1) % 5 == 0 && index > 0 && changeStr.length() > index + 1) {
                        //  index--;
                    } else {
                        index = changeStr.length() - afterStr.length() + index;
                        if (afterStr.length() % 5 == 0 && changeStr.length() > index + 1) {
                            index++;
                        }
                    }
                }
                et.setText(changeStr);
            }
        });

    }

    /**
     * 电话号码输入监听
     *
     * @param et
     */
    public static void initPhoneListener(final EditText et) {
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                beforeStr = s.toString();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                afterStr = s.toString();
                if (changeIndex)
                    index = et.getSelectionStart();
            }

            @Override
            public void afterTextChanged(Editable s) {

                    //输入后
                    String phoneStr = "";
                    String c[] = s.toString().split(" ");
                    for (int i = 0; i < c.length; i++) {
                        phoneStr = phoneStr + c[i];
                    }
                    if (phoneStr.length() > 7) {
                        String str1 = phoneStr.substring(0, 3);
                        String str2 = phoneStr.substring(3, 7);
                        String str3 = phoneStr.substring(7, phoneStr.length());
                        et.removeTextChangedListener(this);
                        et.setText(str1 + " " + str2 + " " + str3);
                    } else if (phoneStr.length() > 3) {
                        String str1 = phoneStr.substring(0, 3);
                        String str2 = phoneStr.substring(3, phoneStr.length());
                        et.removeTextChangedListener(this);
                        et.setText(str1 + " " + str2);
                    } else {
                        et.removeTextChangedListener(this);
                        et.setText(phoneStr);
                    }
                    et.setSelection(et.length());
                    et.addTextChangedListener(this);
                }
        });

    }

}
